package com.zsj.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsj.entity.OrderResponse;
import com.zsj.entity.RechargeResponse;
import com.zsj.enums.*;
import com.zsj.exception.BusinessException;
import com.zsj.mysql.entity.DyMobileRecharge;
import com.zsj.mysql.service.IDyMobileRechargeService;
import com.zsj.util.MoneyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @Description:
 * @Author:
 * @Date:
 */
@Slf4j
@Service
public class ConsoleService {

    @Autowired
    private IDyMobileRechargeService mobileRechargeService;

    @Autowired
    private MobileService mobileService;

    public OrderResponse findOrder(String chargeNo, String month) {
        OrderResponse response = new OrderResponse();
        DyMobileRecharge record;
        if (StringUtils.isBlank(month)) {
            QueryWrapper<DyMobileRecharge> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("charge_no", chargeNo);
            record = mobileRechargeService.getOne(queryWrapper);
        } else {
            record = mobileRechargeService.getByOrderNo(month, chargeNo);
        }
        // 再根据自己订单号查
        if (ObjectUtils.isEmpty(record)) {
            if (StringUtils.isBlank(month)) {
                QueryWrapper<DyMobileRecharge> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("channel_charge_no", chargeNo);
                record = mobileRechargeService.getOne(queryWrapper);
            } else {
                record = mobileRechargeService.getBySelfOrderNo(month, chargeNo);
            }
        }
        if (ObjectUtils.isEmpty(record)) {
            log.info("根据订单号{}未查询到订单", chargeNo);
            throw new BusinessException(ErrorCodeEnum.ORDER_NOT_EXIST);
        }
        BeanUtils.copyProperties(record, response);
        // 映射
        if (StringUtils.isNotBlank(record.getAdCode())) {
            response.setAdCode(AdCodeEnum.findByCode(record.getAdCode()).getName());
        }
        if (StringUtils.isNotBlank(record.getCarrierCode())) {
            response.setCarrierCode(CarrierCodeEnum.findByCode(record.getCarrierCode()).getMsg());
        }
        if (StringUtils.isNotBlank(record.getOriginCarrierCode())) {
            response.setOriginCarrierCode(CarrierCodeEnum.findByCode(record.getOriginCarrierCode()).getMsg());
        }
        if (StringUtils.isNotBlank(record.getCurrentCarrierCode())) {
            response.setCurrentCarrierCode(CarrierCodeEnum.findByCode(record.getCurrentCarrierCode()).getMsg());
        }
        if (StringUtils.isNotBlank(record.getOrderStatus() + "")) {
            response.setOrderStatus(OrderStatusEnum.findByCode(record.getOrderStatus() + "").getMsg());
        }
        if (StringUtils.isNotBlank(record.getNotifyStatus() + "")) {
            response.setNotifyStatus(NotifyStatusEnum.findByCode(record.getNotifyStatus()).getMsg());
        }
        // 金额转为元
        response.setProductDenomination(MoneyUtil.fenToYuan(record.getProductDenomination()));
        response.setContractPrice(StringUtils.isBlank(record.getContractPrice()) ? "" : MoneyUtil.haoToYuan(record.getContractPrice()));
        response.setSupplierContractPrice(StringUtils.isBlank(record.getSupplierContractPrice()) ? "" : MoneyUtil.haoToYuan(record.getSupplierContractPrice()));
        return response;
    }

    public boolean batchReorder(List<String> chargeNoList, String month) {
        try {
            for (String chargeNo : chargeNoList) {
                try {
                    DyMobileRecharge record = mobileRechargeService.getByOrderNo(month, chargeNo);
                    if (record == null) {
                        log.info("不存在充值记录, chargeNo[{}], month[{}]", chargeNo, month);
                        continue;
                    }
                    MDC.put("dyOrderNo", record.getChargeNo());
                    MDC.put("selfUid", record.getChannelChargeNo());
                    if (StringUtils.isNotBlank(record.getSupplierOrderNo())) {
                        log.info("订单:{}已经重发，无需再发送，供应商订单号:{}", record.getChannelChargeNo(), record.getSupplierOrderNo());
                        continue;
                    }
                    log.info("重发的订单:[{}]", JSONObject.toJSONString(record));
                    RechargeResponse response = new RechargeResponse();
                    mobileService.reorder(record, response);
                    Thread.sleep(5);
                } catch (Exception e) {
                    log.info("重发订单异常");
                    log.error("重发订单异常", e);
                } finally {
                    MDC.clear();
                }
            }
            return true;
        } catch (Exception e) {
            log.error("批量重发异常", e);
            return false;
        }
    }
}
